package com.hnkywl.jip.service;

import com.hnkywl.jip.dao.OtherVoucherDao;
import com.hnkywl.jip.dao.VoucherDao;
import com.hnkywl.jip.dao.VoucherType;
import com.hnkywl.jip.interceptor.AuthInterceptor;
import com.hnkywl.jip.utils.Freemarker2Xml;
import com.hnkywl.jip.utils.InterfaceParam;
import com.hnkywl.jip.utils.Md5;
import com.jiuqi.gmc.IVoucherGenerateSvr;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-2-26
 * Time: 下午4:25
 * To change this template use File | Settings | File Templates.
 */
@Service
public class OtherVoucherService {
    private OtherVoucherDao otherVoucherDao;
    private VoucherDao voucherDao;
    private InterfaceParam interfaceParam;
    private IVoucherGenerateSvr voucherGenerateSvr;



    @Transactional
    public Page<Object[]> findOtherVoucher(Date startTime, Date endTime, Integer companyId, Integer bill,
                                           Integer status, Integer voucher, String operator, Integer limit, Integer start){
        return this.otherVoucherDao.findOtherVoucher(startTime, endTime, companyId, bill, status, voucher, operator, limit, start);
    }


    @Transactional
    public Map getVoucher(Integer id, String uuid) {
        Map<String, Object> result = new HashMap<String, Object>();
        List<Object[]> list = otherVoucherDao.getVoucher(id);
        //凭证分录信息
        List<Map> items = new ArrayList<Map>();
        int order = 1;
        for (Object[] item : list) {
            Map<String, Object> voucherItem = new HashMap<String, Object>();
            voucherItem.put("id", order);//唯一标识
            voucherItem.put("vchrid", uuid);//凭证主表ID
            voucherItem.put("itemorder", order);//分录编号
            voucherItem.put("subjectcode", item[1] != null ? item[1] : "");//科目代码
            voucherItem.put("subjectname", item[2] != null ? item[2] : "");//科目名称
            voucherItem.put("debit", item[3] != null ? item[3].toString() : "");//借方金额
            voucherItem.put("credit", item[4] != null ? item[4].toString() : "");//贷方金额
            voucherItem.put("qty", "0");//数量
            voucherItem.put("price", "0");//单价
            voucherItem.put("currencycode", "CNY");//货币代码
            voucherItem.put("exchrate", "1.000000");//汇率
            voucherItem.put("orgnd", item[3] != null ? item[3].toString() : "");//借方原币
            voucherItem.put("orgnc", item[4] != null ? item[4].toString() : "");//贷方原币
            voucherItem.put("settlementcode", "");//结算方式代码
            voucherItem.put("settlementno", "");//结算号
            voucherItem.put("bizdate", item[11] != null ? item[11] : "");//业务日期
            voucherItem.put("digest", item[0] != null ? item[0] : "");//摘要
            voucherItem.put("bizno", "");//业务号
            voucherItem.put("suppliercode", item[8] != null ? item[8] : "");//供应商代码
            voucherItem.put("departmentcode", "");//部门代码
            voucherItem.put("customercode", item[7] != null ? item[7] : "");//客户代码
            voucherItem.put("staffcode", "");//职员代码
            items.add(voucherItem);
            order++;
        }
        result.put("items", items);
        return result;
    }


    //取该凭证的表头
    @Transactional
    public Map getVoucherMaster(Integer id, Integer attachment, String uuid, String year, String mouth,Map userInfo) {
        Map<String, Object> voucherMaster = new HashMap<String, Object>();
        List result = otherVoucherDao.getVoucherMaster(id);
        if (result == null) {
            throw new RuntimeException("没该公司的对照信息!");
        }
        voucherMaster.put("id", uuid);
        voucherMaster.put("vchrtypecode", result.get(0));//凭证类别
        voucherMaster.put("acctyear", year != null ? year : "");//会计年度
        voucherMaster.put("acctperiod", mouth != null ? mouth : "");//会计期间
        voucherMaster.put("createdate", new Date());//凭证日期
        voucherMaster.put("unitcode", result.get(2));//组织机构代码
        voucherMaster.put("companyname", result.get(1));//公司名称
        voucherMaster.put("acctbookcode", "01");//账薄标识
        voucherMaster.put("createuser", userInfo.get(AuthInterceptor.USER_NAME_SESSION_KEY));//制单人
        voucherMaster.put("attachment", attachment);//附件张数
        voucherMaster.put("memo", "");//备注
        return voucherMaster;
    }



    @Transactional
    public Boolean beginGenerateVoucher(Integer id, VoucherType type) {
        return voucherDao.beginGenerateVoucher(id, type);
    }

    @Autowired
    public void setOtherVoucherDao(OtherVoucherDao otherVoucherDao) {
        this.otherVoucherDao = otherVoucherDao;
    }
    @Autowired
    public void setVoucherDao(VoucherDao voucherDao) {
        this.voucherDao = voucherDao;
    }
}
